﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@page language="java" pageEncoding="UTF-8"%>
<%@ page import="com.mysql.jdbc.Driver"%>
<%@ page import="java.sql.*"%>

<%
String userName="root";
String userpasswd="";
String dbName="a0713225307";
    request.setCharacterEncoding("UTF-8");
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	String u="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userpasswd+"&useUnicode=true&characterEncoding=UTF-8";
    java.sql.Connection con=java.sql.DriverManager.getConnection(u,"root","");
	String sql="select * from message_db order by mId desc";
	Statement statement=con.createStatement();
	ResultSet rs=statement.executeQuery(sql);

%>
<style>
.table-striped>tbody>tr:nth-child(odd){background-color:rgba(255,255,255,0.5)!important;}
</style>

<div  style="height:610px;width:100%;">

<table id="content-table4" class="table table-striped">
<tr style="width:100%;">
<th class="col-md-1">id</th>
<th class="col-md-2">发布时间</th>
<th class="col-md-2">发布者</th>
<th class="col-md-6">信息</th>
<th class="col-md-1"></th>
</tr>

<tbody id="tb4"style="width:100%;">
<%
while(rs.next()){

%>	
<tr class="tr4">
<td class="col-md-1">
<%
out.print(rs.getString(1));
%>
</td>
<td class="col-md-2">
<%
out.print(rs.getString(2));
%>
</td>
<td class="col-md-2">
<%
out.print(rs.getString(3));
%>
</td>
<td class="col-md-6">
<div style="width:250px;height:50px;line-height:25px;overflow:hidden;">
<%
out.print(rs.getString(4));
%>
</div>
</td>
<td class="col-md-1" style="text-align:right;vertical-align:middle;">
<img src="../images/delete.png" style="width:20px;height:20px;visibility:hidden;"onclick="deleteMsg(event);" />
</td>
</tr>
<%
}
%>	

</tbody>
</table>
</div>

<div id="page_nav4">
<span id="spanFirst3">第一页</span>
		<span id="spanPre3">上一页</span>
		<span id="spanNext3"><a href="javascript:next4();">下一页</a></span>
		<span id="spanLast3"><a href="javascript:last4();">最后一页</a></span>
		第
		<span id="spanPageNum3">1</span>
		页/共
		<span id="spanTotalPage3">2</span>
		页
</div>
<%
rs.close();
statement.close();
con.close();
%>
<script>

var all_row4=$('#tb4 tr');

for(var i=8;i<all_row4.length;i++){
	
	all_row4.eq(i).css("display","none");
}


var theTable4 = document.getElementById("tb4");
var totalPage4 = document.getElementById("spanTotalPage3");
var pageNum4 = document.getElementById("spanPageNum3");

var spanPre4 = document.getElementById("spanPre3");
var spanNext4 = document.getElementById("spanNext3");
var spanFirst4 = document.getElementById("spanFirst3");
var spanLast4 = document.getElementById("spanLast3");

var numberRowsInTable4 = theTable4.rows.length;
var pageSize4 = 8;
var page4 = 1;

//下一页
function next4() {

	hideTable4();
	
	currentRow4 = pageSize4 * page4;
	maxRow4 = currentRow4 + pageSize4;
	if ( maxRow4 > numberRowsInTable4 )
	maxRow4 = numberRowsInTable4;
	for ( var i = currentRow4; i< maxRow4; i++ ) {
		theTable4.rows[i].style.display = '';
	}
		page4++;
	if ( maxRow4 == numberRowsInTable4 ){
		nextText4();
		lastText4();
	}
	showPage4();
	preLink4();
	firstLink4();
}

//上一页
function pre4() {

	hideTable4();
	
	page4--;

	currentRow4 = pageSize4 * page4;
	maxRow4 = currentRow4 - pageSize4;
	if ( currentRow4 > numberRowsInTable4 )
	currentRow4 = numberRowsInTable4;
	for ( var i = maxRow4; i< currentRow4; i++ ) {
		theTable4.rows[i].style.display = '';
	}


	if ( maxRow4 == 0 ) {
		preText4();
		firstText4();
	}
	
	showPage4();
	nextLink4();
	lastLink4();
}

//第一页
function first4() {
	hideTable4();
	page4 = 1;
	for ( var i = 0; i<pageSize4; i++ ) {
		theTable4.rows[i].style.display = '';
	}
	showPage4();

	preText4();
	nextLink4();
	lastLink4();
}

//最后一页
function last4() {
	hideTable4();
	page4 = pageCount4();
	currentRow4 = pageSize4 * (page4 - 1);
	for ( var i = currentRow4; i<numberRowsInTable4; i++ ) {
		theTable4.rows[i].style.display = '';
	}
	showPage4();
	
	preLink4();
	nextText4();
	firstLink4();
}

function hideTable4() {
	for ( var i = 0; i<numberRowsInTable4; i++ ) {
	theTable4.rows[i].style.display = 'none';
	}
}

function showPage4() {
	pageNum4.innerHTML = page4;
}

//总共页数
function pageCount4() {
	var count4 = 0;
	if ( numberRowsInTable4%pageSize4 != 0 ) count4 = 1; 
	return parseInt(numberRowsInTable4/pageSize4) + count4;
}

//显示链接
function preLink4() { spanPre4.innerHTML = "<a href='javascript:pre4();'>上一页</a>"; }
function preText4() { spanPre4.innerHTML = "上一页"; }

function nextLink4() { spanNext4.innerHTML = "<a href='javascript:next4();'>下一页</a>"; }
function nextText4() { spanNext4.innerHTML = "下一页"; }

function firstLink4() { spanFirst4.innerHTML = "<a href='javascript:first4();'>第一页</a>"; }
function firstText4() { spanFirst4.innerHTML = "第一页"; }

function lastLink4() { spanLast4.innerHTML = "<a href='javascript:last4();'>最后一页</a>"; }
function lastText4() { spanLast4.innerHTML = "最后一页"; }

//隐藏表格
function hide4() {
	for ( var i = pageSize4; i<numberRowsInTable4; i++ ) {
		theTable4.rows[i].style.display = 'none';
	}

	totalPage4.innerHTML = pageCount4();
	pageNum4.innerHTML = '1';
	
	nextLink4();
	lastLink4();
}

hide4();

var x4=$('.tr4');
	for(var j=0;j<x4.length;j++){
		x4[j].onmouseover=function(){
			$(this).attr("class", "highlight");
			 var imgb=$(this).find('img');
	 imgb.css("visibility","visible");
		}
		
		
   x4[j].onmouseout = function() {
	   $(this).removeAttr("class");
	   var imgb=$(this).find('img');
	   imgb.css("visibility","hidden");
 // $(this).attr("class", "odd"); //鼠标离开时还原之前的class值
 //  }
	}
	}

function deleteMsg(e){
	e=e||window.event;
	e.target=e.target||e.srcElement;
	var p=$(e.target).parent().prev().prev().prev().prev().text();
	var param="msgId='"+p+"'";
	
	$.ajax({
		type:'POST',
		url:'deleteMsg.jsp',
		data:param,
		success:function(){
			window.location.href=window.location.href;
		}
	});
}
</script>